<?php
	include("header.php");
	
	require_once("lib/document.php");
	require_once("lib/sql.php");
	require_once("lib/util.php");
?>
<?php

	$pageTitle ="View Documents List";
			
	$util=new DBUtil();
	$conn=$util->getConnection();
	$sql = SQLQuery::DOCUMENT_ALL_SQL;
	$stmt = $conn->prepare( $sql);
	$stmt->execute();
			
	$result = $stmt->get_result();
	$documents = array();
			
	if ($result->num_rows > 0) {
		while($row = $result->fetch_array()) {
		$document = new Document();
		$document->setId($row["ID"]);
		$document->setName($row["NAME"]);
		$document->setDescription($row["DESCRIPTION"]);
		$document->setPeriod($row["PERIOD"]);
		array_push($documents,$document);
			}
		}
	$conn->close();
			

?>

<script type="text/javascript">
function updateDetails(){

	//Delete all rows except the first row(index 0)
	$("#documentdetails > table > tbody > tr").each(function(index){
		if(index > 0){
			$(this).remove();
		}
	});
	//Get the data from backend
	$.getJSON("lib/managedocuments.php?fulldetails=yes",function(data,status){
	
		$.each(data, function(counter, document) {
			var documentId=document.id;
			var documentName=document.name;
			var documentDescription=document.description;
			var documentPeriod=document.period;
 
			var bodyelement=$("#documentdetails > table > tbody");
			var updateStr= '<a id="documentId'+documentId+ '" href="#" onclick="javascript:updateDialog(\'Update Document\',\'lib/managedocuments.php\',\'lib/managedocuments.php\',\''+documentId+'\',\'updateDetails()\')"><span>Edit Document</span><div class="modify"></div></a>';
			var deleteStr= '<a id="documentId'+documentId+ '" href="#" onclick="javascript:deleteDialog(\'Delete Document\',\'lib/managedocuments.php\',\'lib/managedocuments.php\',\''+documentId+'\',\'updateDetails()\')"><span>Remove Document</span><div class="delete"></div></a>';
			
			var attachstatutoryStr= '<a id="documentId'+ documentId+ '"href=\"#\"  onClick=\"javascript:updateDialog(\'Attach Statutory\', \'lib/attachstatutory.php\',\'lib/attachstatutory.php\',\''+documentId+'\',\'updateDetails()\')">Attach Statutory</a>';
			
						
			var rowString='<tr><td>'+(counter+1) +'</td><td>'+documentName+'</td><td>'+documentDescription+'</td><td>'+documentPeriod+'</td><td>'+updateStr+'</td><td>'+deleteStr+'</td><td>'+ attachstatutoryStr +'</td></tr>';
			$(rowString).appendTo(bodyelement);
		});
		
		//var pane = $('#documentdetails');
		//var api = pane.data('jsp');
		//api.reinitialise();
	});
}
</script>


<div class="maincontent">
<!------- Header ---------->
<div class="borderedDiv" >
	<table style="width:100%">
		<tr>
			<td class="titleText blueheadertext">
				View Documents
			</td>
		</tr>
	</table>
</div>

<!------- Content ---------->

<div class="scroll-pane displaytable" id="documentdetails">
	<table class="contenttable">
	<thead>
		<tr>
			<td style="width:8%">Sl. No.</td>
			<td>Name</td>
			<td style="width:50%">Description</td>
			<td style="width:50%">Period</td>
			<td style="width:20px">&nbsp;</td>
			<td style="width:20px">&nbsp;</td>
			<td style="width:20px">&nbsp;</td>
		</tr>
	</thead>
	
	<tbody>
	<tr>
		<td colspan="7" style="text-align: right;"><a href="#" onClick="javascript:createDialog('Create Document', 'lib/managedocuments.php','lib/managedocuments.php','updateDetails()')"><span>Add Document</span><div id="addDocument" class="add" style="float:right;"></div> </a></td>
	</tr>
	
	<?php
	for($i=0;$i < sizeof($documents);$i++){
		$document=$documents[$i];
		$documentId=$document->getId();
		$documentName=$document->getName();
		$documentDescription=$document->getDescription();
		$documentPeriod=$document->getPeriod();
				
		echo ("<tr>");
		echo ("<td>" .($i+1) ."</td>");
		echo ("<td>" .$documentName ."</td>");
		echo ("<td>" .$documentDescription ."</td>");
		echo ("<td>" .$documentPeriod ."</td>");
		echo "<td><a id=\"documentid_" .$documentId  ."\" alt=\"Edit\" href=\"#\" onClick=\"javascript:updateDialog('Update Document', 'lib/managedocuments.php','lib/managedocuments.php'," .$documentId .",'updateDetails()')\"><span>Edit Document</span><div class=\"modify\"></div></a></td>";
		echo "<td><a id=\"documentid_" .$documentId  ."\" href=\"#\"  onClick=\"javascript:deleteDialog('Delete Document', 'lib/managedocuments.php','lib/managedocuments.php'," .$documentId .",'updateDetails()')\"><span>Remove Document</span><div class=\"delete\"></div></a></td>";
		echo "<td><a id=\"documentid_" .$documentId  ."\" href=\"#\"  onClick=\"javascript:updateDialog('Attach Statutory', 'lib/attachstatutory.php','lib/attachstatutory.php'," .$documentId .",'updateDetails()')\">Attach Statutory</a></td>";
		echo ("</tr>");

	}
?>

	</tbody>
	</table>
</div>
</div>

